import requests
import pymysql

f=open("password.txt","r")
password=f.read()
f.close()
password=password.replace("\n","")
conn = pymysql.connect(host='localhost', user='root',
                       password=password, database='teamfight_wiki', port=3306)
cursor = conn.cursor()
base_url = "https://game.gtimg.cn/images/lol/act/img/tft/js/"
url = base_url + "chess.js"
f = open("英雄.txt", "w",encoding="utf-8")
count = 0
response = requests.get(url)
data = response.json()
for i in data["data"]:
    count += 1
    attribute = "https://game.gtimg.cn/images/lol/act/img/tft/champions/"+i["name"] + "$" + f"""生命：{i["lifeData"]},攻击：{i["attackData"]},护甲：{i["armor"]},攻速：{i["attackSpeed"]},法强:{i["magic"]}"""
    cost=i["price"]
    hero_skill=i["skillDetail"]
    if len(attribute) > 200:
        attribute = attribute[:200]
    if len(hero_skill) > 200:
        hero_skill = hero_skill[:200]
    print(count, i["title"]+" "+i["displayName"], attribute,cost,hero_skill)
    sql = f"""INSERT INTO hero VALUES({count},"{i["title"]+" "+i["displayName"]}","{attribute}",{cost},"{hero_skill}");"""
    cursor.execute(sql)
conn.commit()
